package com.xiaotu.spider.letv.pageprocessor;

import com.xiaotu.common.exception.SpiderException;
import com.xiaotu.common.exception.SpiderExceptionCode;
import com.xiaotu.common.model.HotTVModel;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.PageUtils;
import com.xiaotu.common.util.PropertiesUtil;
import com.xiaotu.spider.AUrlPageProcessor;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.selector.Html;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/***
 * 
 * 项目名称：Sprider-WebMagic    
 * 类名称：LeTVHotPageProcessor    
 * 类描述： 乐视网站热剧爬取   
 * 创建人：张晓   
 * 创建时间：2019年12月25日 下午3:45:23    
 * @version
 */
@Component("LeTVHotPageProcessor")
@Scope("prototype")
public class LeTVHotPageProcessor extends AUrlPageProcessor
{
	//最新(来源于app)
    private static final String BASE_NEW_URL_1 =
            "http://static.meizi.app.m.letv.com/android/mod/mob/ctl"
            + "/listalbum60/allow_risk_album/true/act/index/src/1/cg/2/ph/420003%2C420004"
            + "/pn/";
    
    private static final String BASE_NEW_URL_2 =
            "/ps/30/pcode/010110041/version/9.13.5/country"
            + "/CN/provinceid/1/districtid/9/citylevel/1/location/%E5%8C%97%E4%BA%AC%E5%B8%82%7C%E6%9C%9D%E9%98%B3%E5%8C%BA/region"
            + "/CN/lang/chs/or/20/ispay/0,1/vt/0/sc/0/ar"
            + "/";
    
    private static final String BASE_NEW_URL_3 =
            "/yr/0.mindex.html";
    
    //最热(来源于app)
    private static final String BASE_HOT_URL_1 =
    		"http://static.meizi.app.m.letv.com/android/mod/mob/ctl"
    		+ "/listalbum60/allow_risk_album/true/act/index/src/1/cg/2/ph/420003%2C420004"
    		+ "/pn/";
    
    private static final String BASE_HOT_URL_2 =
    		"/ps/30/markid/e130c7d60ee8e423d8b24cac5d8f28b2/pcode/010110041/version/9.13.5/country"
    		+ "/CN/provinceid/1/districtid/9/citylevel/1/location/%E5%8C%97%E4%BA%AC%E5%B8%82%7C%E6%9C%9D%E9%98%B3%E5%8C%BA/region"
    		+ "/CN/lang/chs/or/9/ispay/0,1/vt/0/sc/0/ar"
    		+ "/";
    private static final String BASE_HOT_URL_3 =
    		"/yr/0.mindex.html";

    private static final String[] AREA_ARR = new String[]{"50001", "50002", "50003"};// 地区：内地、香港、台湾

    @Override
    protected String[] getStartRequestUrls()
    {
        int pageMaxSize = PropertiesUtil.getPropertyByInt(Constants.SpiderConfigKey.HOTTV_PAGE_SIZE);
        List<String> urlList = new ArrayList<String>();
        Arrays.asList(AREA_ARR).forEach(area -> {
        	for(int i= 1;i<=pageMaxSize;i++){
        		urlList.add(BASE_NEW_URL_1+i+BASE_NEW_URL_2+area+BASE_NEW_URL_3);
        		urlList.add(BASE_HOT_URL_1+i+BASE_HOT_URL_2+area+BASE_HOT_URL_3);
        	}
        });
        String[] urlArr = new String[urlList.size()];
        return urlList.toArray(urlArr);
    }

    @Override
    protected String getSpiderType()
    {
        return Constants.SpiderTaskType.LeTV_HOT;
    }

    @Override
    public String getPageDataType(Page page)
    {
    	String url = page.getRequest().getUrl();
    	if(url.contains("http://static.meizi.app.m.letv.com/android")){
    		return Constants.PageDataType.SEARCH_LIST;
    	}else if(url.contains("http://www.le.com/tv")){
    		return Constants.PageDataType.TV_BASICINFO;
    	}
    	return null;
    }

    @Override
    public void process(Page page)
    {
    	
    	Map<String, Object> extraMap = page.getRequest().getExtras();
    	
    	switch (this.getPageDataType(page))
        {
            case Constants.PageDataType.TV_BASICINFO:
                this.actorsProcess(page,extraMap);
                break;
            case Constants.PageDataType.SEARCH_LIST:
            	this.tvListProcess(page,extraMap);
            	break;
        }
    }
    
    /** tvListProcess(剧列表)   
	 * 创建人：张晓   
	 * 创建时间：2019年12月25日 下午3:54:24    
	 * 修改备注： 
	 * @param page
	 * @param extrasMap  
	 */
	private void tvListProcess(Page page, Map<String, Object> extraMap) {
		
		String json = page.getRawText();
        if (StringUtils.isEmpty(json))
            throw new SpiderException(
                    SpiderExceptionCode.DataInvalideException);

        JSONObject jsonObj = JSONObject.fromObject(json);
        if (jsonObj == null || jsonObj.isEmpty() || !jsonObj.has("body"))
            throw new SpiderException(
                    SpiderExceptionCode.DataInvalideException);
        JSONArray items = jsonObj.getJSONObject("body").getJSONArray("album_list");
        if (items.size() < 1)
            throw new SpiderException(
                    SpiderExceptionCode.DataInvalideException);
        for (int i = 0; i < items.size(); i++)
        {
            JSONObject tvJson = items.getJSONObject(i);
            String url = String.format("http://www.le.com/tv/%s.html",tvJson.getString("aid"));
            String aid = tvJson.getString("aid");
            String tvname = tvJson.getString("name");
            extraMap = new HashMap<String, Object>();
            extraMap.put("url", url);
            extraMap.put("aid", aid);
            extraMap.put("tvname", tvname);
            PageUtils.addTargetRequest(page, url, extraMap);
        }
        
	}

	/** actorsProcess(剧演员信息)   
	 * 创建人：张晓   
	 * 创建时间：2019年12月25日 下午3:53:09    
	 * 修改备注： 
	 * @param page
	 * @param extrasMap  
	 */
	private void actorsProcess(Page page, Map<String, Object> extrasMap) {
		List<String> liSpanList = page.getHtml().xpath("//div[@class='top_con']//ul[@id='j-ulInfo']//li").all();
		liSpanList.stream().anyMatch(li -> {
			boolean flag = false;
			Html liHtml = new Html(li);
			String title = liHtml.xpath("//b/text()").get();
			if(null != title && title.contains("主演")){
				List<HotTVModel> hotList = new ArrayList<HotTVModel>();
				String actors = liHtml.xpath("//span/@title").get();
				hotList.add(new HotTVModel(Constants.SiteName.LETV,
						extrasMap.get("tvname").toString(),
						this.getActors(actors),
						extrasMap.get("url").toString()));
				page.putField(HotTVModel.class.getName(), hotList);
				flag = true;
			}
			return flag;
		});
	}

	private List<String> getActors(String actors)
    {
    	List<String> actorList = new ArrayList<String>();
    	if(null == actors || StringUtils.isBlank(actors)){
    		return actorList;
    	}
    	actors = actors.replace(" ", "");
    	String [] actorArr = actors.split("/");
        for (int i = 0; i < actorArr.length; i++)
            actorList.add(actorArr[i]);
        return actorList;
    }
}
